package com.jmb.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jmb.api.common.exception.LeaseException;
import com.jmb.api.common.result.ResultCodeEnum;
import com.jmb.api.model.entity.Db;
import com.jmb.api.model.vo.db.DbVO;
import com.jmb.api.service.DbService;
import com.jmb.api.mapper.DbMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author jinbo
* @description 针对表【db】的数据库操作Service实现
* @createDate 2025-04-08 13:53:56
*/
@Service
public class DbServiceImpl extends ServiceImpl<DbMapper, Db>
    implements DbService{

    @Autowired
    private DbMapper dbMapper;


    /**
     * 保存
     * @param dbDao 数据库
     */
    @Override
    public void saveDb(Db dbDao) {
        LambdaQueryWrapper<Db> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Db::getName,dbDao.getName());
        if (this.count(queryWrapper)>0){
            throw  new LeaseException(ResultCodeEnum.WEB_API_DB_NAME_EXIST);
        }
        this.save(dbDao);
    }

    /**
     * 根据id查询
     * @param id 传入的id
     * @return 返回数据信息
     */
    @Override
    public Db getDbId(Integer id) {
        if (id == null){
            throw new LeaseException(ResultCodeEnum.WEB_API_ID_NOT_NULL);
        }
        LambdaQueryWrapper<Db> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Db::getId,id);
        return this.getOne(queryWrapper);
    }

    @Override
    public IPage<Db> ListDao(IPage<Db> page, DbVO dbVO) {
        return dbMapper.selectPageDb(page,dbVO);
    }

    @Override
    public void modifyDb(Db dbDao) {

    }

    @Override
    public void deleteDb(Integer id) {

    }

    @Override
    public String dbConnectInfo(Integer id) {
        return "";
    }

    /**
     * 根据id查询数据
     * @param id 传入的id
     * @return 返回单条数据对象
     */
    @Override
    public DbVO findDbById(Integer id) {
        return dbMapper.selectDbById(id);
    }
}




